const net = require('net');
// const aes = require('../lib/aes');
// const md5 = require('../lib/md5');
const uuid = require('../lib/uuid');
// const key = '47e43adc6f31eae15b359186fb198256e41e749b38a1963e7e9ba20551a7bf3aa9ce4e69de7e36de17bd9695e88eea3117e1aae02122fa846ee29722ed17e395ea9d82ed8117bac775b11afaaaa7cf3746be24ef658db23e8e8123b8e7e1da863f997aa2ee8e71fad94743ea9ee731a5ebec86becb49e7b6eae6e7';//1ce7213246
// const iv = '42aaeda0a68e724337eefd7658939ed4ea048afe96319f3628985ae74b7eee52a89a9ebce5a7ee86c62a612bfeb5bc9ed6465bec18eba472ddefdebb1ae163c2a496efb1beee1b14933ee197f2fe1e8b81d76e121273d1eb79e75b5847523377eeaa1177713ae4a1da189198a51aa4ca7e6c92862eee9e716a737f';//1e33ae8392
console.time('connect');
let conn = net.connect({
  host: 'iy88.site',
  port: 1223
}, () => {
  console.timeEnd('connect');
  conn.on('end', () => {
    console.log('end');
    process.exit(-1)
  })
  conn.on('close', () => {
    console.log('close');
    process.exit(-1)
  })
  conn.on('error', () => {
    console.log('error');
    process.exit(-1)
  })
  // let salt = md5(new Date().getFullYear + '-' + (new Date().getMonth + 1) + '-' + new Date().getDate()).slice(0, 10);
  // let c = aes(key + salt, iv + salt);
  console.time('auth');
  conn.write(JSON.stringify({t: 'auth', d: 'ljx', cid: uuid(), f: 'user'}));
  conn.on('data', buffer => {
    let data = JSON.parse(buffer.toString());
    if (data.t === 'auth') {
      // salt = data.s;
      // c = aes(key + salt, iv + salt);
      console.timeEnd('auth');
      console.time('show');
      conn.write(JSON.stringify({t: 'show', d: {t: 'node'}, cid: uuid()}));
    } else if (data.t === 'show') {
      if (data.d.length > 0) {
        console.time('get');
        conn.write(JSON.stringify({
          t: 'get', d: {
            n: data.d[0], req: {
              c: 'GET / HTTP/1.1\n' +
                  'Host: localhost\n' +
                  'Connection: keep-alive\n' +
                  'Cache-Control: max-age=0\n' +
                  'Upgrade-Insecure-Requests: 1\n' +
                  'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36\n' +
                  'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\n' +
                  'Sec-Fetch-Site: cross-site\n' +
                  'Sec-Fetch-Mode: navigate\n' +
                  'Sec-Fetch-Dest: document\n' +
                  'Accept-Language: zh-CN,zh;q=0.9,ru;q=0.8\n' +
                  'Cookie: Hm_lvt_79dc1243945dcce03ecead690ed493d9=1593092022,1593144701,1593148674,1593149515\n' +
                  '\n',
              h: 'iy88.site',
              p: 433,
              s: true
            }
          }, cid: uuid()
        }));
      } else {
        console.log('show: no node found');
      }
      console.timeEnd('show');
    } else if (data.t === 'get') {
      if (data.ret === 1) {
        // console.log('--------------------\n'+decodeURI(data.data.headers)+'\n--------------------');
        // console.log(decodeURI(data.data.body));
        console.log(data.data);
        console.log('get: response');
      } else {
        console.log('get: fail');
      }
      console.timeEnd('get');
    }
  })
})
